#!/bin/bash

## Copyright (C) 2021 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

#set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose

# shellcheck source=../../../../../helper-scripts/usr/libexec/helper-scripts/has.sh
## Sibling repo: absent in an isolated CI checkout, so shellcheck cannot
## follow it there. The source= path above still documents where it lives.
##
## TODO: We really should get rid of these "not following" silence directives
## and just run shellcheck only when a derivative-maker checkout is available.
## The above source directive had a bug (one too few "../") previously, but AI
## didn't catch it, most likely because it silenced the error that bug
## triggered with the below directive.
# shellcheck disable=SC1091
source /usr/libexec/helper-scripts/has.sh

## Check if the VirtualBox host software is installed.
if ! has vboxmanage ; then
   ## VirtualBox host software is not installed.
   if test -f /etc/sysctl.d/30-lkrg-virtualbox.conf ; then
      ## Delete using '--verbose' so user is notified.
      safe-rm --force --verbose -- /etc/sysctl.d/30-lkrg-virtualbox.conf
   fi
   exit 0
fi

if ! test -d /etc/sysctl.d ; then
   exit 0
fi

if ! test -f /usr/share/security-misc/lkrg/30-lkrg-virtualbox.conf ; then
   exit 0
fi

if ! package-installed-check "lkrg" ; then
   exit 0
fi

## Delete using '--verbose' so user is notified.
cp --verbose -- /usr/share/security-misc/lkrg/30-lkrg-virtualbox.conf /etc/sysctl.d/30-lkrg-virtualbox.conf
